﻿using System.Text;
using System.Web.Mvc;

namespace TSharp.Core.Mvc.Web.Filters
{
    /// <summary>
    ///     异常过滤器示例
    ///     <para>2010/9/16</para>
    ///     <para>TANGJINGBO</para>
    ///     <author>tangjingbo</author>
    /// </summary>
    public class ExceptionFilter : FilterAttribute, IExceptionFilter
    {
        /// <summary>
        ///     异常过滤器
        /// </summary>
        public static readonly ExceptionFilter Instance = new ExceptionFilter();

        #region IExceptionFilter Members

        /// <summary>
        ///     在发生异常时调用。
        /// </summary>
        /// <param name="filterContext">筛选器上下文。</param>
        public void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;
            string sorryHtml = string.Format("<html><body>{0}</body></html>", filterContext.Exception.Message);
            filterContext.Result = new ContentResult
                                       {
                                           ContentType = "text/html",
                                           ContentEncoding = Encoding.UTF8,
                                           Content = sorryHtml
                                       };
        }

        #endregion
    }
}